import 'package:flutter/material.dart';
import 'package:top_foundation/utils/theme/theme.dart';

import '../../utils/util.dart';

class LineProgress extends StatefulWidget {
  const LineProgress({
    super.key,
    this.progress = 0,
    required this.onChanged,
  });
  final double progress;
  final ValueChanged<double> onChanged;
  @override
  State<LineProgress> createState() => _LineProgressState();
}

class _LineProgressState extends State<LineProgress> {
  double start = 0;
  double update = 0;
  double progress = 0;
  double _endProgress = 0;
  @override
  void initState() {
    _endProgress = widget.progress;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(builder: (context, cons) {
      return GestureDetector(
        onPanStart: (details) {
          start = details.localPosition.dx;
        },
        onPanUpdate: (details) {
          update = details.localPosition.dx - start;
          Get.log('update    $update\n--total   ${cons.biggest.width}');
          progress = _endProgress + update / cons.biggest.width;
          if (progress < 0) {
            progress = 0;
          }
          if (progress > 1) {
            progress = 1;
          }
          Get.log('progress   $progress');
          setState(() {});
          widget.onChanged(progress);
        },
        onPanEnd: (details) {
          _endProgress = progress;
        },
        behavior: HitTestBehavior.translucent,
        child: SizedBox(
          height: 20,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              LinearProgressIndicator(
                value: progress,
                color: ITheme.selectedColor,
                backgroundColor: ITheme.disableBgColor,
                minHeight: 2,
              ),
            ],
          ),
        ),
      );
    });
  }
}
